<template>
  <!-- 目标：它从无到有是有动画过渡效果
    <transition name="down">: vue会元素可见和不可见的关键节点 自动补充 特殊的css类名
  -->
  <Transition name="down">
    <div class="messageTips-CSS" :style="style" v-show="visible">
      <!-- 上面绑定的是样式 -->
      <!-- 不同提示图标会变 -->
      <i class="iconfont" :class="[style[type].icon]"></i>
      <span class="text">{{ text }}</span>
    </div>
  </Transition>
</template>
<script>
import { onMounted, ref } from "vue";

export default {
  name: "messageTips",
  props: {
    text: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      // warn 警告  error 错误  success 成功
      default: "warn",
    },
  },
  setup() {
    // 定义一个对象，包含三种情况的样式，对象key就是类型字符串
    const style = {
      warn: {
        icon: "icon-warning",
        color: "#E6A23C",
        backgroundColor: "rgb(253, 246, 236)",
        borderColor: "rgb(250, 236, 216)",
      },
      error: {
        icon: "icon-error",
        color: "#F56C6C",
        backgroundColor: "rgb(254, 240, 240)",
        borderColor: "rgb(253, 226, 226)",
      },
      success: {
        icon: "icon-success",
        color: "#67C23A",
        backgroundColor: "rgb(240, 249, 235)",
        borderColor: "rgb(225, 243, 216)",
      },
      info: {
        icon: "icon-info",
        color: "#67C23A",
        backgroundColor: "rgb(240, 249, 235)",
        borderColor: "rgb(225, 243, 216)",
      },
    };

    const visible = ref(false);
    onMounted(() => {
      visible.value = true;
    });

    return { style, visible };
  },
};
</script>
<style scoped lang="less">
.messageTips-CSS {
  width: 300px;
  height: 50px;
  position: fixed;
  z-index: 9999;
  left: 50%;
  margin-left: -150px;
  top: 25px;
  line-height: 50px;
  padding: 0 25px;
  border: 1px solid #e4e4e4;
  background: #f5f5f5;
  color: #999;
  border-radius: 4px;
  i {
    margin-right: 4px;
    vertical-align: middle;
  }
  .text {
    vertical-align: middle;
  }
}
.iconfont {
  width: 20px;
  height: 20px;
  border-radius: 100%;
  background: #fff;
  display: inline-block;
  margin-right: 20px;
  &.icon-warning {
    background: url("../../assets/image/icon-warning.png") no-repeat center
      center;
    background-size: auto 100%;
  }
  &.icon-error {
    background: url("../../assets/image/icon-error.png") no-repeat center center;
    background-size: auto 100%;
  }
  &.icon-success {
    background: url("../../assets/image/icon-success.png") no-repeat center
      center;
    background-size: auto 100%;
  }
  &.icon-info {
    background: url("../../assets/image/icon-info.png") no-repeat center center;
    background-size: auto 100%;
  }
}
.down {
  &-enter {
    &-from {
      transform: translate3d(
        -100%,
        0,
        0
      ); //scale(0);//translate3d(0, -75px, 0);
      opacity: 0;
    }
    &-active {
      transition: all 0.2s; //all 0.5s;
    }
    &-to {
      transform: translate3d(0, 0, 0); //scale(1);//none;
      opacity: 1;
    }
  }
}
</style>